const schemaJoi = require('./util/schemaJoi.js');
const Joi = require('joi');
const service = require('./service/service.js');

exports.isMember = async (context) => {
  try {
    const result = await context.cloud.topApi.invoke({
         api: 'taobao.crm.member.identity.get',
         data : {  
           mix_nick:context.mixNick// mixNick 和 nick 必须填写一个  
           },  
           autoSession: true,
    })
    return {success:true,res:result}  
  } catch (e) {
     return {success:false,res:e}  
  } 
};

exports.getMemberUrl = async (context) => {
  const { cloud, mixNick, data } = context
  try {
     return await service.getMemberUrl(data.url, cloud);
  } catch (e) {
     return {success:false,res:e}  
  } 
};


exports.addUser = async (context) => {
  const { cloud, mixNick, data } = context
  try {
    //参数校验
    let params = {
      mixNick: mixNick,
      nickName: data.nickName,
      avatar: data.avatar
    }
    const schema = Joi.object().keys({
      mixNick: Joi.string().required(),
      nickName: Joi.string().required(),
      avatar: Joi.string().required(),
    });
    await schemaJoi(schema, params);

    return await service.addUser(params, cloud);
  } catch (err) {
    return { isOk: 0, msg: '铺捉到错误' + err };
  }
}


//   //查看用户中奖
exports.findUserWinning = async (context) => {
  const { cloud, mixNick, data } = context
  try {
    //参数校验
    let params = {
      mixNick: mixNick,
    }
    return await service.findUserWinning(params, cloud);
  } catch (err) {
    return { isOk: 0, msg: '铺捉到错误' + err };
  }
}

//   //填写用户中奖信息
exports.updateUserWinning = async (context) => {
  const { cloud, mixNick, data } = context
  try {
    //参数校验
    let params = {
      mixNick: mixNick,
      name: data.name,
      phone: data.phone,
      address: data.address,
      nickName: data.nickName
    }
    const schema = Joi.object().keys({
      mixNick: Joi.string().required(),
      nickName: Joi.string().required(),
      name: Joi.string().required(),
      phone: Joi.string().required(),
      address: Joi.string().required(),
    });
    await schemaJoi(schema, params);
    return await service.updateUserWinning(params, cloud);
  } catch (e) {
    return { isOk: 0, msg: '铺捉到错误' + err };
  }
}

// 中奖列表
exports.findWinning = async (context) => {
  const { cloud, mixNick, data } = context
  try {
    return await service.findWinning(cloud);
  } catch (e) {
    return { isOk: 0, msg: '铺捉到错误' + err };
  }
}

// 查看加购信息
exports.findCart = async (context) => {
  const { cloud, mixNick, data } = context
  try {
    return await service.findCart(mixNick,cloud);
  } catch (e) {
    return { isOk: 0, msg: '铺捉到错误' + err };
  }
}

//   // 加入购物车
exports.addCart = async (context) => {
  const { cloud, mixNick, data } = context
  try {

    let params = {
      mixNick: mixNick,
      itemId: data.itemId,
    }
    const schema = Joi.object().keys({
      mixNick: Joi.string().required(),
      itemId: Joi.string().required(),
    });
    //参数校验
    await schemaJoi(schema, params);
    return await service.addCart(params, cloud);
  } catch (e) {
    return { isOk: 0, msg: '铺捉到错误' + err };
  }
}

//    // 抽奖
exports.winning = async (context) => {
  const { cloud, mixNick, data } = context
  try {

    return await service.winning(mixNick,cloud);
  } catch (e) {
    return { isOk: 0, msg: '铺捉到错误' + err };
  }
}




/*
* 添加埋点信息 
* @return data: object 埋点的数据
* */
exports.addUserStat = async (context) => {
  const { cloud, data, mixNick, userNick } = context
  console.log("============================", data)
  try {
    //参数验证
    const schema = Joi.object().keys({
      stat_data: Joi.object().required(),
    });
    await schemaJoi(schema, data);
    let paramsData = data.stat_data;
    paramsData.userId = mixNick;
    return await service.addUserStat(JSON.stringify(paramsData), cloud);
  } catch (e) {
    return { isOk: 0, msg: '铺捉到错误' + e };
  }
};



